<?php

 /**
  * @package JooS
  * @subpackage PHPackager
  */

  require_once "JooS/PHPackager/DOM/Element.php";

  class JooS_PHPackager_DOM_Element_Control extends JooS_PHPackager_DOM_Element {
    /**
     * @var array
     */
    private $_packages = array();

    /**
     * @return JooS_PHPackager_DOM_Element_Control
     */
    public function make() {
      $this->_packages = array();
      $_packages = $this->query("package");
      
      foreach ($_packages as $package) {
        /* @var $package JooS_PHPackager_DOM_Element_Package */
        $package->make();

        $name = $package->getName();
        if (isset($this->_packages[$name])) {
          require_once "JooS/PHPackager/DOM/Exception.php";

          throw new JooS_PHPackager_DOM_Exception("Project name must be unique");
        }
        else {
          $this->_packages[$name] = $package;
        }
      }
      return $this;
    }

    /**
     * @return JooS_PHPackager_DOM_Element_Control
     */
    public function install() {
      foreach ($this->_packages as $package) {
        /* @var $package JooS_PHPackager_DOM_Element_Package */
        
        $package->install();
      }
      return $this;
    }
  }
